﻿using Leo.Blog.BLL.SysModule;
using Leo.Blog.Common.JsonHelper;
using Leo.Blog.ViewModel;
using Leo.Blog.ViewModel.SysModule;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Leo.Blog.Controllers
{
    public class RoleManageController : BaseController
    {
        // GET: RoleManage
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetRoles(int page = 1, int rows = 30)
        {
            int totalCount = 0;
            List<SysRoleVM> listRoles = Sys_Roles_B.GetRoles(page, rows, out totalCount);
            var obj = new
            {
                total = totalCount,
                rows = listRoles,
            };
            var toJson = JsonHelper.SerializeObject(obj);
            return Content(toJson);
        }
        public ActionResult GetSysRolesTree(int roleId,int parentId=-1)
        {
            //获取角色有的模组权限
            List<SysRoleModuleVM> roleModules = Sys_RoleModule_B.GetRoleModuleByRoleId(roleId);
            //获取该账号所具有权限的所有的菜单模块节点
            UserAccountVM accountVM = Session["UserAccount"] as UserAccountVM;
            List<UserModule> list = accountVM.Modules;
            TreeNode node = new TreeNode();
            GetRoleToModuleTree(node, list, parentId, roleModules);
            string toJson = JsonHelper.SerializeObject(node.Children);
            return Content(toJson);
        }
        /// <summary>
        /// 根据角色设置模组生成树
        /// </summary>
        /// <param name="node"></param>
        /// <param name="list"></param>
        /// <param name="parentId"></param>
        /// <param name="roleModules"></param>
        private void GetRoleToModuleTree(TreeNode rootNode, List<UserModule> listModules, int parentId, 
            List<SysRoleModuleVM> roleModules)
        {
            var listParent = Sys_Modules_B.GetUserModulesByParentId(listModules, parentId);
            foreach (var item in listParent)
            {
                var listChild = Sys_Modules_B.GetUserModulesByParentId(listModules, item.ModuleId);
                TreeNode tn = new TreeNode();
                tn.Id = item.ModuleId;
                tn.Text = item.ModuleName ;
                tn.State = "open";
                //tn.Attributes = new { GrantedCode = "" };//赋予按钮权限用
                if (roleModules != null && roleModules.Count > 0)
                {
                    for (int i = 0, count = roleModules.Count; i < count; i++)
                    {
                        int itemModule = item.ModuleId;
                        int roleModule = roleModules[i].ModuleId.Value;
                        if (itemModule.Equals(roleModule))
                        {
                            //只有叶子节点需要设置checked
                            if (listChild.Count == 0)
                            {
                                tn.IsChecked = true;
                            }
                            //var temp1 = new { GrantedCode = dsRole.Tables[0].Rows[i]["GrantedCode"] };
                            //tn.Attributes = temp1;
                        }
                    }
                }
                if (listChild != null)
                {
                    GetRoleToModuleTree(tn, listModules, item.ModuleId, roleModules);
                }
                rootNode.Children.Add(tn);
            }
        }

        [HttpPost]
        public ActionResult Add(SysRoleVM model)
        {
            model.CreateBy = this.UserId;
            SysRoleVM obj = Sys_Roles_B.AddorUpdate(model);
            if (obj != null)
            {
                return Json(ResCode.Success);
            }
            return Json(ResCode.Fail);
        }
        /// <summary>
        /// 删除，以","区分开
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(string ids)
        {
            if (Sys_Roles_B.DeleteList(ids))
            {
                return Json(ResCode.Success);
            }
            return Json(ResCode.Fail);
        }
        [HttpPost]
        public ActionResult UpdateIsEnable(int roleId)
        {
            bool res = Sys_Roles_B.UpdateIsEnable(roleId, this.UserId);
            if (res)
            {
                return Json(ResCode.Success);
            }
            return Json(ResCode.Fail);
        }

        [HttpPost]
        public ActionResult SaveRoleModule(int RoleId, string Modules)
        {
            bool res = Sys_RoleModule_B.SaveRoleModule(RoleId, Modules,this.UserId);
            if (res)
            {
                return Json(ResCode.Success);
            }
            return Json(ResCode.Fail);
        }
    }
}
